package com.apollo.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;


/**
 * Swagger2配置类
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket controllerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.apollo.demo.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalResponseMessage(RequestMethod.GET, getResponseMessages())
                .globalResponseMessage(RequestMethod.POST, getResponseMessages());
    }

    public ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot整合Swagger2项目接口文档")
                .description("该项目基于SpringBoot、MyBatis和Swagger2构建")
                .version("1.0.0")
                .build();
    }

    public List<ResponseMessage> getResponseMessages() {
        return newArrayList(
            new ResponseMessageBuilder().code(200).message("Successful — 请求已完成").build(),
            new ResponseMessageBuilder().code(400).message("请求中有语法问题，或不能满足请求").build(),
            new ResponseMessageBuilder().code(401).message("未授权客户机访问数据").build(),
            new ResponseMessageBuilder().code(403).message("资源不可用").build(),
            new ResponseMessageBuilder().code(404).message("服务器找不到给定的资源；文档不存在").build(),
            new ResponseMessageBuilder().code(500).message("服务器不能完成请求").build(),
            new ResponseMessageBuilder().code(501).message("服务未完成").build()
        );
    }

}
